package u1project.server;

import java.io.*;
import java.lang.reflect.Method;
import java.net.Socket;

import u1project.common.Request;
import u1project.common.Response;

public class ServerThread extends Thread {
	private Socket s;
	private UserManager userManager = new UserManager();

	public ServerThread(Socket s) {
		this.s = s;
	}

	public Response exit(Request req) {
		return new Response(true, "退出了");
	}

	public Response login(Request req) {
		Response<String, Object> resp = null;
		String name = (String) req.get("name");
		String pass = (String) req.get("pass");
		// if (name.contains("liu") && pass.contains("123")) {
		// resp = new Response<String, Object>(true, "登录成功，欢迎" + name);
		// } else {
		// resp = new Response<String, Object>(false, "错误的用户名或者密码");
		// }
		// return resp;
		boolean f = userManager.login(name, pass);
		resp = new Response<String, Object>(f, f ? "登录成功，欢迎" + name : "错误的用户名或者密码");
		return resp;
	}

	public Response reg(Request req) {
		Response<String, Object> resp = null;
		String name = (String) req.get("name");
		String pass = (String) req.get("pass");
		String pass2 = (String) req.get("repeatpass");
		boolean f = userManager.reg(name, pass, pass2);
		resp = new Response<String, Object>(f, f ? "注册成功" : "注册失败：可能是用户名重复或者是两次密码不一致");
		return resp;
	}

	String base = "D:\\我的文档\\My Pictures\\";

	// 发送文件给客户端，包括文件内容和文件名
	public Response download(Request req) throws Exception {
		Response<String, Object> resp = null;
		String name = (String) req.get("filename");

		if (name == null) {
			resp = new Response(false, "文件名没有提供");
			return resp;
		}
		File f = new File(base, name);

		if (f.exists() == false) {
			resp = new Response(false, "目录不存在");
		} else if (f.isDirectory()) {
			resp = new Response(false, "目录无法下载");
		} else {
			FileInputStream fin = new FileInputStream(f);
			byte[] b = new byte[fin.available()];
			fin.read(b);
			fin.close();
			resp = new Response(true, "下载成功");
			resp.put("content", b);
			resp.put("filename", f.getName());
		}
		return resp;

	}

	// 上传给我
	// fileName,content
	// 写到本地磁盘上
	public Response upload(Request req) throws Exception {
		Response<String, Object> resp = null;
		String name = (String) req.get("filename");
		byte[] b = (byte[]) req.get("content");
		try {
			FileOutputStream f = new FileOutputStream("E:/Temp/" + name);
			f.write(b);
			f.close();
			resp = new Response(true, "成功接收了文件：" + name);
			// System.out.println("保存文件成功");
		} catch (Exception e) {
			resp = new Response(false, "接收发生了异常：" + e);
		}
		return resp;

	}

	@Override
	public void run() {
		System.out.println(s + "连接了。。。");
		// 服务端的线程为一个客户端服务，直到退出

		try {
			InputStream in = s.getInputStream();
			OutputStream out = s.getOutputStream();
			ObjectInputStream oin = new ObjectInputStream(in);
			ObjectOutputStream oout = new ObjectOutputStream(out);
			Request<String, Object> req = null;
			Response<String, Object> resp = null;
			while (true) {
				req = (Request) oin.readObject();
				// try {
				Method m = ServerThread.class.getDeclaredMethod(req.getAction(), Request.class);
				resp = (Response<String, Object>) m.invoke(this, req);
//				} catch (Exception e) {
//					// TODO 自动生成的 catch 块
//					// e.printStackTrace();
//					resp = new Response<String, Object>(false, "没有实现的操作:" + e);
//				}

//				if (req.getAction().equals("login")) {
//					resp = login(req);
//				} else if (req.getAction().equals("download")) {
//					resp = download(req);
//				} else if (req.getAction().equals("upload")) {
//					resp = upload(req);
//				} else {
//					resp = new Response<String, Object>(false, "没有实现的操作");
//				}
				System.out.println(">>>got:" + resp.getMsg());
				oout.writeObject(resp);
				oout.flush();
				oout.reset();// !!!!!!!!
				if (req.getAction().equals("exit"))
					break;
			}
			// connection was reset
		} catch (Exception e) {
			System.out.println(s + " 出错了 " + e);
		} finally {
			if (s != null)
				try {
					System.out.println(s + " 退出了。。。");
					s.close();
				} catch (Exception e) {

					// e.printStackTrace();
				}
		}
	}
}
